// https://leetcode.cn/problems/maximum-repeating-substring/
// Created by ade on 2022/11/3.
//
#include <string>
#include <iostream>

using namespace std;

class Solution {
public:
    int maxRepeating1(string sequence, string word) {
        if (sequence.size() < word.size()) return 0;
        if (sequence.size() == word.size()) return sequence == word;
        int num = sequence.size() / word.size();
        string tmp = "";
        for (int i = 0; i < num; i++)tmp += word;

        for (int i = num; i >= 1; i--) {
            tmp = tmp.substr(0, i * word.size());
            if (sequence.find(tmp) != string::npos) return i;
        }
        return 0;
    }

    // 暴力解法
    int maxRepeating(string sequence, string word) {
        int sLen = sequence.size(), wLen = word.size();
        if (sLen < wLen) return 0;
        if (sLen == wLen) return sequence == word;
        int num = sLen / wLen;
        string tmp = "";
        for (int i = 0; i < num; i++) tmp += word;

        for (int i = num; i >= 1; i--) {
            tmp = tmp.substr(0, i * wLen);
            for (int j = 0; j <= sLen - wLen; j++) {
                int flag = 1;
                for (int k = 0; k < i * wLen; k++) {
                    if (sequence[j + k] != tmp[k]) {
                        flag = 0;
                        break;
                    }
                }

                if (flag == 1) return i;
            }
        }
        return 0;
    }
};

int main() {
    Solution so;
    string a = "bbba";
    string b = "aa";
    int n = so.maxRepeating(a, b);
    cout << "size:" << n << endl;
    return 0;
}